home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Boxer / TZUtils / stdio2.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  848 b   |  54 lines

  1. #include "stdio2.h"
  2.  
  3. static unsigned char lastchr, lastung;
  4.  
  5. int fgetc(FILE * fp)
  6. {
  7.   if (lastung) {
  8.     lastung = 0;
  9.     return lastchr;
  10.   }
  11.   if (1 == fread(&lastchr, 1, 1, fp))
  12.     return lastchr;
  13.   else
  14.     return -1;
  15. }
  16.  
  17. int ungetc(int c, FILE * fp)
  18. {
  19.   lastung = c;
  20.   lastchr = c;
  21.   return c;
  22. }
  23.  
  24. int fputc(int c, FILE * fp)
  25. {
  26.   unsigned char c1 = c;
  27.   fwrite(&c1, 1, 1, fp);
  28.   return c1;
  29. }
  30.  
  31. int fgets(char *c, int l, FILE * fp)
  32. {
  33.   int lorg = l;
  34.   if (l)
  35.     *c = 0;
  36.   while (l--) {
  37.     if (1 != fread(c, 1, 1, fp) || *c++ == '\n')
  38.       break;
  39.   }
  40.   if (l)
  41.     *c = 0;
  42.   return lorg - l;
  43. }
  44.  
  45. FILE *fopen(char *name, char *mode)
  46. {
  47.   lastung = 0;
  48.  
  49.   // Modes need lots of work
  50.   return FileOpen(0, name, 'DATA', 'UNIX',
  51.                   (*mode == 'r' ? fileModeReadOnly : fileModeReadWrite)
  52.                   | fileModeAnyTypeCreator, NULL);
  53. }
  54.